package com.ruoyi.shop.controller;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.shop.domain.CheShop;
import com.ruoyi.shop.service.ICheShopService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.RemoteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @ClassName ShopRegisterController
 * @Description TODO
 * @Author xgj
 * @Date 2025年01月08日 上午10:27
 * @Version 1.0
 */
@RestController
@RequestMapping("/register")
public class ShopRegisterController {
    @Autowired
    private ICheShopService cheShopService;
    @Autowired
    private RemoteUserService remoteUserService;


    @PostMapping("/regist")
    public AjaxResult rgUser(@RequestBody CheShop cheshop){
        if (cheshop!=null){
            System.out.println("cheshop:"+cheshop);
            SysUser user = new SysUser();
            user.setUserName(cheshop.getPerName());
            user.setPassword("$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2");
            user.setPhonenumber(cheshop.getPerPhone());
            user.setNickName("商家");
            user.setLoginIp("127.0.0.1");
            user.setRemark("商户");
            //  先注册sys-user表
            R<Boolean> booleanR = remoteUserService.registerUserInfo(user, SecurityConstants.INNER);

            System.out.println("user:"+user);
            System.out.println(booleanR.getData());

            //  再注册che-shop表
            cheshop.setCreatTime(new Date());
            cheshop.setName(cheshop.getPerName());
            cheshop.setPerPasword("admin123");
            cheshop.setUserId(user.getUserId());
            int i = cheShopService.insertCheShop(cheshop);
            if (i>0){
                cheShopService.updateUserId(cheshop.getPerPhone());
                return AjaxResult.success(cheshop);
            }else {
                System.out.println("che-shop出错");
                return AjaxResult.error("注册失败");
            }

        }
        return AjaxResult.error("出错了");
    }


}
